package org.lzy.shop.activity

import android.graphics.BitmapFactory
import android.text.TextUtils
import android.util.Base64
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.core.graphics.toColorInt
import com.google.android.material.textview.MaterialTextView
import com.kongzue.dialogx.dialogs.BottomDialog
import com.kongzue.dialogx.dialogs.TipDialog
import com.kongzue.dialogx.dialogs.WaitDialog
import com.kongzue.dialogx.interfaces.OnBindView
import org.lzy.shop.Constant
import org.lzy.shop.R
import org.lzy.shop.base.BaseMvvmActivity
import org.lzy.shop.databinding.ActivityRegisterBinding
import org.lzy.shop.util.StringUtils
import org.lzy.shop.viewmodel.LoginViewModel

/**
 * 注册
 */
class RegisterActivity: BaseMvvmActivity<ActivityRegisterBinding, LoginViewModel>({ ActivityRegisterBinding.inflate(it) }){



    override fun initView() {
        setTitleText(getResString(R.string.reg_btn))
    }

    override fun initData() {
      //图片验证码
        mViewModel.picSmsLiveData.observe(this){picSmsResponse ->
            dismissLoading()
            picSmsResponse?.let {
                BottomDialog.show(object : OnBindView<BottomDialog?>(R.layout.picsmsbottomdialog) {
                    override fun onBind(dialog: BottomDialog?, view: View) {
                        val editCode = view.findViewById<EditText>(R.id.pic_sms_num)
                        val img = view.findViewById<ImageView>(R.id.pic_sms_show)
                        // Base64解码
                        val decodedBytes = Base64.decode(picSmsResponse?.data!!.replace("data:image/png;base64", ""), Base64.DEFAULT)
                        // 创建Bitmap
                        val decodedBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size)
                        img.setImageBitmap(decodedBitmap)
                        val sureBtn = view.findViewById<MaterialTextView>(R.id.btnSure)
                        sureBtn.setOnClickListener {
                            if (TextUtils.isEmpty(editCode.text.toString())) {
                                showToast(getResString(R.string.pic_sms_hint))
                            } else {
                                dialog?.dismiss()
                                mViewModel.sendSms(binding.actEditLoginPwdEtTel.text.toString(), picSmsResponse.captchaId!!, editCode.text.toString()
                                )
                            }
                        }
                    }
                }).setAllowInterceptTouch(false).setMaskColor("#4D000000".toColorInt())
            }
        }

        //图片验证后再次发送验证码
        mViewModel.smsLiveData.observe(this){str ->
            dismissLoading()
            binding.actEditLoginPwdEtPswOld.setText(str)
        }

        mViewModel.registerLiveData.observe(this){string ->
            dismissLoading()
            TipDialog.show(string, WaitDialog.TYPE.SUCCESS)
            startKillActivity(LoginActivity::class.java)

        }
    }

    override fun requestData() {

    }

    override fun allClick() {
        binding.countdown.setOnClickListener {
            loadInput()
        }

        binding.regBtn.setOnClickListener {
            val mobile = binding.actEditLoginPwdEtTel.text.toString().trim()  //手机
            val old = binding.actEditLoginPwdEtPswOld.text.toString().trim() //旧密码
            val psw1 = binding.actEditLoginPwdEtPswOne.text.toString().trim()  //新密码
            val psw2 = binding.actEditLoginPwdEtPswTwo.text.toString().trim()  //确认新密码

            if (StringUtils.isEmpty(mobile)) {
                showToast(getResString(R.string.input_mobile))
                return@setOnClickListener
            }
            if (StringUtils.isEmpty(old)) {
                showToast(getResString(R.string.Please_enter_old_password))
                return@setOnClickListener
            }
            if (StringUtils.isEmpty(psw1)) {
                showToast(getResString(R.string.Please_enter_new_password))
                return@setOnClickListener
            }
            if (StringUtils.isEmpty(psw2)) {
                showToast(getResString(R.string.please_input_password_again))
                return@setOnClickListener
            }
            //两次密码不一致时
            if (psw1 != psw2) {
                showToast(getResString(R.string.password_nomatch))
                return@setOnClickListener
            }
            showLoading()
            mViewModel.register(mobile, old, psw1, psw2)

        }

    }

    fun loadInput() {
        val userMobile = binding.actEditLoginPwdEtTel.text.toString()
        if (TextUtils.isEmpty(userMobile)) {
            showToast(getString(R.string.mobile_empty))
            return
        }
        if (userMobile.length < Constant.ELEVEN) {
            showToast(getResString(R.string.mobile_error))
            return
        }

        if (binding.countdown.getmCurrentmillis() > Constant.TWOTHOURSANDS) {
            showToast((binding.countdown.getmCurrentmillis() / 1000).toString() +getResString(R.string.try_again_in_seconds) )
            return
        }
        showLoading()
        mViewModel.getPicSms()
    }




}